var shopcartInit = function() {};

$(function() {
	var html = [];
	var allChecked = true;
	shopcartInit.prototype = {

		getFooter: function() {
			var previousPage = "";
			if (AGENT_CLIENT.APP) {
				previousPage = plus.webview.currentWebview().preUrl;
			} else {
				previousPage = getUrlParam("url");
			}
			//从商品详情跳转过来
			if ((/shoppingcart.html/.test(previousPage))) {
				console.log(1);
				$("#back").on("click", function() {
					back();
				});
			} else {
				$("#back").children("i:first").hide();
				$("head").append('<script src="../../js/common/footer.js" type="text/javascript"></script>');
			}
		},
		//店铺
		paintCarthtml: function(data) {
			if (null == data) {
				$("#main").html('<div class="zhanwushuju"><img src="../../images/zanwushuju.png"><br>暂无数据</div>');
				return;
			}
			if (AGENT_CLIENT.APP) {
				if (0 == data.count) {
					$("#editCart>span:first").html('编辑');
				}
				plus.webview.getWebviewById("../home/main.html").evalJS("getShopcartProductCount()");
				var previousPage = "";
				if (AGENT_CLIENT.APP) {
					previousPage = plus.webview.currentWebview().preUrl;
				} else {
					previousPage = getUrlParam("url");
				}
				if ((/shoppingcart.html/.test(previousPage))) {
					plus.webview.currentWebview().opener().evalJS('getShopcartProductCount()');
				}
			}

			supplier = data.supplier;
			var i = 0,
				shop = data.goods,
				strStyle = "",
				total = 0,
				totalCoin = 0,
				subTotal = 0,
				subCoinTotal = 0,
				checkedProductCount = 0;
			//      if (data.platformno) {
			//        html.push('<p class="qcmyh mbbottom">全场满¥699减100，并且免收邮费</p>');
			//      }
			//店铺
			while (supplier[i]) {
				html.push('<div id="shop_' + i + '" ' + strStyle + ' class="shop-group-item mbtopbottom">');
				html.push('<div class="shop-title mbbottom">');
				html.push('<div class="item">');
				html.push('<div class="shop-title-content">');
				html.push('<span class="shop-title-name">');
				html.push('<b class="checkboxFour">');
				var shopChecked = true;
				var j = 0;
				var goods_suites = supplier[i].goods; //: supplier[i].suites
				if (null != goods_suites) {
					while (goods_suites[j]) {
						shopChecked = shopChecked && ((goods_suites[j].is_checked == 1));
						j++;
					}
				}
				allChecked = allChecked && shopChecked;
				if ((supplier[i].goods && supplier[i].goods.length > 0) || (supplier[i].suites && supplier[i].suites.length > 0)) {
					html.push('<input type="checkbox" value="1" ' + ((true == shopChecked) ? 'checked="checked"' : '') + ' id="checkbox-1-1_shop_' + i + '" name="RadioGroup1" class="checkbox-gx">');
					html.push('<label for="checkbox-1-1_shop_' + i + '"></label>');
				}
				html.push('</b>' + supplier[i].shop_name);
				html.push('</span>');
				html.push('<div class="shop-title-detail">');
				html.push(this.pingSupplierAction(i, supplier[i]));
				html.push('</div>');
				html.push('</div>');
				html.push('</div>');
				html.push('</div>');

				var j = 0;
				var k = 0;
				var subCoinTotal = 0;
				var thisAmount = 0;
				var thisCoin = 0;
				var thisCount = 0;
				var isChecked = false;
				var goods = [];
				//套件        
				if (supplier[i].suites && supplier[i].suites.length > 0) {
					var suites = supplier[i].suites;
					j = 0;
					while (suites[j]) {
						html.push(this.paintSuites(i, j, suites[j], 'suite'));
						isChecked = suites[j].is_checked;
						thisCoin = isChecked ? (suites[j].qty * suites[j].total_coin) : 0;
						thisCount = isChecked ? suites[j].qty : 0;
						subCoinTotal += suites[j].qty * suites[j].total_coin;
						totalCoin += thisCoin;
						checkedProductCount += isChecked ? (suites[j].qty * suites[j].goods.length) : 0;;
						j++;
					}
				}
				//商品
				if (supplier[i].goods && supplier[i].goods.length > 0) {
					goods = supplier[i].goods;
					while (goods[j]) {
						isChecked = goods[k].is_checked;
						thisCoin = isChecked ? (goods[j].qty * goods[j].coin) : 0;
						thisCount = isChecked ? goods[j].qty : 0;
						html.push(this.paintGoods(i, j, goods[j], 'goods'));
						subCoinTotal += goods[j].qty * goods[j].coin;
						totalCoin += thisCoin;
						checkedProductCount += thisCount;
						j++;
					}
				}

				//选择的满减，满增活动
				if (supplier[i].gifts && supplier[i].gifts.length > 0) {
					this.paintSelectedGifts(goods.gifts[0]);
				}
				if (supplier[i].current_full_subtract && supplier[i].full_subtract && supplier[i].full_subtract.length > 0) {
					this.paintSelectedFullsubtract(supplier[i].current_full_subtract, supplier[i].full_subtract);
				}

				if ((supplier[i].goods && supplier[i].goods.length > 0) || (supplier[i].suites && supplier[i].suites.length > 0)) {
					html.push('<p class="shp-heji">');
					html.push('<span>小计：</span>');
					html.push('<span id="subTotal_' + i + '"  class="money">&yen;' + supplier[i].subtotal_money + '</span>+');
					html.push('<span class="jinbi"><b><img src="../../images/jbtub.jpg"></b>');
					html.push('<span id="subCoinTotal_' + i + '">' + totalCoin + '</span></span>');
					html.push('</p>');
				}
				html.push('</div>');
				i++;
			}

			$("#main").html(html.join(''));
			var footerHeight = $("#footbg").height();
			$("#goCalculate").css({
				"bottow": footerHeight + "px"
			});
			$("#totalCoin").html(data.total_coin);
			$("#total").html(data.total_money);
			$("#goodsCount").html(checkedProductCount);
			$("#checkbox-1-1_selectAll").prop("checked", allChecked);
			html = [];
		},
		//商品
		paintGoods: function(i, j, goods, suiteType) {
			html.push('<ul id="goods_' + i + '_' + j + '"  class="shp-cart-list">');
			html.push('<li class="shp-gwc mbbottom">');
			html.push('<div class="items">');
			html.push('<div class="check-wrapper">');
			html.push('<b class="checkboxFour">');
			var checked = (goods.is_checked == 1);
			var tips = {
				type: suiteType,
				sku_id: goods.sku_id,
				qty: goods.qty,
			};
			html.push('<input tip=' + JSON.stringify(tips) + ' type="checkbox" ' + ((1 == goods.is_checked) ? 'checked="checked"' : '') + ' id="checkbox-1-1_goods_' + i + '_' + j + '" name="RadioGroup2" class="checkbox-gx" >');
			html.push('<label for="checkbox-1-1_goods_' + i + '_' + j + '"></label>');
			html.push('</b>');
			html.push('</div>');
			//商品名称
			html.push('<div class="shp-cart-item-core shp-gwcny">');
			html.push('<a name="goodsItem" tip="' + goods.sku_id + '" class="cart-product-cell-1" href="javascript:void(0);">');
			html.push('<img class="cart-photo-thumb" src="' + getImageUrl(goods.logo, 59, 59) + '">');
			html.push('</a>');
			html.push('<div name="goodsItem" tip="' + goods.sku_id + '" class="cart-product-cell-2">');
			html.push('<div name="productName" class="cart-product-name1 cart-product-name2">');
			html.push('<span class="w40">' + goods.name + '</span>');
			html.push('</div>');

			//添加减少个数
			html.push('<div name="addReduce" class="shp-cart-opt hide" >');
			html.push('<div class="quantity-wrapper">');
			html.push('<a tip="' + goods.cart_id + '" id="reduce_' + i + '_' + j + '" class="quantity-decrease color-hei" href="javascript:void(0);">-</a>');
			html.push('<input type="text" id="quantity_' + i + '_' + j + '" value="' + goods.qty + '" class="quantity" size="4" placeholder="0">');
			html.push('<a tip="' + goods.cart_id + '" id="add_' + i + '_' + j + '" class="quantity-increase" href="javascript:void(0);">+</a>');
			html.push('</div>');
			html.push('</div>');

			//商品规格
			html.push('<div class="cart-product-name w40"><a href="javascript:void(0);">');
			var spec = goods.spec,
				k = 0;
			if (spec[k]) {
				html.push('<span>' + spec + '</span>');
			}
			html.push('</a></div>');

			//商品价格和抵用星币
			var act_price = (goods.act_price != goods.shop_price) ? goods.act_price : goods.shop_price;
			html.push('<span id="price_' + i + '_' + j + '" class="money">&yen;' + act_price + '</span>+');
			html.push('<span class="jinbi">');
			html.push('<b><img src="../../images/jbtub.jpg"></b>');
			html.push('<span id="coin_' + i + '_' + j + '">' + goods.coin + '</span></span>');
			html.push('<span id="qty_' + i + '_' + j + '" style="float: right;font-size: 1.4rem;color: #333333;">x' + goods.qty + '</span>');
			html.push('</div>');
			var tips = {
				price: act_price,
				coin: goods.coin,
				qty: goods.qty,
				sku_id: goods.sku_id,
				cart_id: goods.cart_id,
			};
			html.push('<div id="delete_' + i + '_' + j + '" tip=' + JSON.stringify(tips) + ' class="delete" style="display:none" name="deleteCurentCart">删除</div>');

			//商品可选的活动列表
			if (goods.promos && goods.promos.length > 0) {
				tips = {
					promotion: PROMOTION_TYPE.promos,
					supplierIndex: i,
					goodsIndex: j
				};
				html.push('<div class="shop-title-detail">');
				html.push('<span name="sales_promotion" tips=' + JSON.stringify(tips) + ' id="promos_' + i + '_' + j + '" class="mbright" class="sales">促销</span>');
				html.push('</div>');
				html.push('</div>');
			}

			//已经参与的商品活动
			if (goods.promos && goods.promos.length > 0 && goods.promo_id && goods.promo_id > 0) {
				var promos = goods.promos;
				var i = 0;
				while (promos[i]) {
					this.paintPromos(goods.promos[i]);
					i++;
				}
			}

			//    html.push('<div class="shp-gwcny">');
			//    html.push('<p class="shp-ms-icon shp-ms1">满送</p>');
			//    html.push('<p class="shp-coudan">全球购买满500减100，立即凑单 </p>');
			//    html.push('</div>');

			html.push('</div>');
			html.push('</li>');
			html.push('</ul>');
		},
		//套件
		paintSuites: function(i, j, suite, suiteType) {
			html.push('<div class="shop-title mbbottom"><div class="item"><div class="shop-title-content">');
			html.push('<span class="shop-title-name">');
			html.push('<b class="checkboxFour">');
			var checked = (1 == suite.is_checked) ? 'checked="checked"' : '';
			var tips = {
				type: suiteType,
				suite_no: suite.suite_no,
				qty: suite.qty,
			};
			html.push('<input type="checkbox" ' + checked + ' tip=' + JSON.stringify(tips) + ' id="checkbox-1-1_goods_' + i + '_' + j + '" name="RadioGroup1" class="checkbox-gx">');
			html.push('<label for="checkbox-1-1_goods_' + i + '_' + j + '"></label>');
			html.push('</b>');
			html.push(suite.suite_name);
			html.push('</span>');
			html.push('</div></div></div>');
			var k = 0,
				goods = {};
			html.push('<ul id="goods_' + i + '_' + j + '" class="shp-cart-list">');
			while (suite.goods[k]) {
				goods = suite.goods[k];
				html.push('<li class="shp-gwc mbbottom">');
				html.push('<div class="items">');
				//商品名称
				html.push('<div class="shp-cart-item-core shp-gwcny">');
				if (goods.is_main == 0) {
					html.push('<p class="shp-ms-icon">套件</p>');
				}
				html.push('<a ' + ((goods.is_main == 1) ? 'name="goodsItem"' : '') + '  tip="' + goods.sku_id + '" class="cart-product-cell-1" href="javascript:void(0);">');
				html.push('<img class="cart-photo-thumb" src="' + getImageUrl(goods.logo, 59, 59) + '">');
				html.push('</a>');
				html.push('<div ' + ((goods.is_main == 1) ? 'name="goodsItem"' : '') + '  tip="' + goods.sku_id + '" class="cart-product-cell-2">');
				html.push('<div ' + ((goods.is_main == 1) ? 'name="productName"' : '') + '  class="cart-product-name1 cart-product-name2">');
				html.push('<span class="w40">' + goods.name + '</span>');
				html.push('</div>');
				// 添加减少个数
				if (goods.is_main == 1) {
					html.push('<div name="addReduce" class="shp-cart-opt hide" >');
					html.push('<div class="quantity-wrapper">');
					html.push('<a tip="' + goods.cart_id + '" id="reduce_' + i + '_' + j + '" class="quantity-decrease color-hei" href="javascript:void(0);">-</a>');
					html.push('<input type="text" id="quantity_' + i + '_' + j + '" value="' + suite.qty + '" class="quantity" size="4" placeholder="0">');
					html.push('<a tip="' + goods.cart_id + '" id="add_' + i + '_' + j + '" class="quantity-increase" href="javascript:void(0);">+</a>');
					html.push('</div>');
					html.push('</div>');
				}
				//商品规格
				html.push('<div class="cart-product-name w40">');
				html.push('<a href="javascript:void(0);">');
				html.push('<span>' + goods.spec + '</span>');
				html.push('</a>');
				html.push('</div>');

				//商品价格和抵用星币
				html.push('<span id="price_' + i + '_' + j + '" class="money">&yen;' + goods.act_price + '</span>+');
				html.push('<span class="jinbi">');
				html.push('<b><img src="../../images/jbtub.jpg"></b>');
				html.push('<span id="coin_' + i + '_' + j + '">' + goods.coin + '</span></span>');
				if (goods.is_main == 1) {
					html.push('<span id="qty_' + i + '_' + j + '" style="float: right;font-size: 1.4rem;color: #333333;">x' + suite.qty + '</span>');
				}
				html.push('</div>');
				if (goods.is_main == 1) {
					var tips = {
						price: suite.total_shopprice,
						coin: 0,
						qty: 1,
						sku_id: suite.suite_no,						
						cart_id: goods.cart_id	
					};
		
					html.push('<div id="delete_' + i + '_' + j + '" tip=' + JSON.stringify(tips) + ' class="delete" style="display:none" name="deleteCurentCart">删除</div>');
				}
				html.push('</div>');
				html.push('</div>');
				html.push('</li>');

				k++;
			}
			html.push('</ul>');
		},
		pingSupplierAction: function(i, supplier) {
			//可选择的满减优惠
			var tips = {};
			if (supplier.full_subtract && supplier.full_subtract.length > 0) {
				tips = {
					promotion: PROMOTION_TYPE.full_subtract,
					supplierIndex: i,
					goodsIndex: null
				};
				html.push('<span name="sales_promotion" tips=' + JSON.stringify(tips) + ' id="full_subtract_' + i + '" class="mbright" class="sales">' + PROMOTION_TYPE.full_subtract.name + '</span>');
			}
			//满赠活动
			if (supplier.full_gift && supplier.full_gift.length > 0) {
				tips = {
					promotion: PROMOTION_TYPE.full_gift,
					supplierIndex: i,
					goodsIndex: null
				};
				html.push('<span name="sales_promotion" tips=' + JSON.stringify(tips) + ' id="full_gift_' + i + '" style="display:inline-block;width:50px;text-align:center;">' + PROMOTION_TYPE.full_gift.name + '</span>');
			}
			//可领取优惠券列表
			if (supplier.coupons && supplier.coupons.length > 0) {
				tips = {
					promotion: PROMOTION_TYPE.coupons,
					supplierIndex: i,
					goodsIndex: null
				};
				html.push('<span name="sales_promotion" tips=' + JSON.stringify(tips) + ' id="coupons_' + i + '" style="display:inline-block;width:50px;text-align:center;">' + PROMOTION_TYPE.coupons.name + '</span>');
			}
		},
		//当前选择的活动
		paintPromos: function(promos) {
			html.push('<div class="shp-gwcny">');
			html.push('<p class="shp-ms-icon shp-ms1">促销</p>');
			html.push('<p class="shp-coudan">' + promos.promo_title + '</p>');
			html.push('</div>');
		},
		//当前选择的满赠活动
		paintSelectedGifts: function(gift) {
			html.push('<div tip="' + gift.skuid + '" class="shp-cart-item-core sub-shp-gwcny">');
			html.push('<p class="shp-ms">换购</p>');
			html.push('<div class="cart-product-cell-3">');
			html.push('<i class="iconfont icon-diancha"></i>');
			html.push('</div>');
			html.push('<a class="sub-cart-product-cell-1" href="javascript:void(0);">');
			html.push('<img class="sub-cart-photo-thumb" src="' + getImageUrl(gift.logo, 59, 59) + gift.logo + '">');
			html.push('</a>');
			html.push('<div class="cart-product-cell-2 ">');
			html.push('<div class=" cart-product-name1"><span>' + gift.name + '</span></div>');
			html.push('<div class="shp-cart-opt">');
			html.push('<p class="sub-wastebin">x' + gift.qty + '</p>');
			html.push('</div>');
			html.push('</div>');
			html.push('</div>');
		},
		//当前选择的满减活动
		paintSelectedFullsubtract: function(currentSubtract, list) {
			var i = 0;
			while (list[i]){
				if(currentSubtract == list[i].promo_id){
					html.push('<div class="shp-gwcny">');
					html.push('<p class="shp-ms-icon shp-ms1">'+ list[i].promo_name +'</p>');
					html.push('<p class="shp-coudan">'+ list[i].promo_title +'</p>');
					html.push('</div>');
					break;
				}
				i++;
			}
		}
	}
})